package cyc2018.贪心思想;

/**
 * 种花问题
 */
public class Test605 {
    // 遍历整个数组，隔空插花，插一个 n-- 最后判断 n 是否为 0
    public static boolean canPlaceFlowers(int[] flowerbed, int n) {
        for (int i = 0; i < flowerbed.length; ++i) {
            // i == 0 与 i == flowerbed.length - 1 用来防止越界
            if (flowerbed[i] != 1
                    && (i == 0 || flowerbed[i - 1] == 0)
                    && (i == flowerbed.length - 1 || flowerbed[i+1] == 0) ) {
                flowerbed[i] = 1;
                n--;
                i++;
            }
        }
        return n <= 0;
    }

    public static void main(String[] args) {
//        int[] a = {1,0,0,0,1};
        int[] a = {0,0,1,0,0};
        int n = 1;
        System.out.println(canPlaceFlowers(a, n));
    }
}
